﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace FlashGames
{
  public class ConfigUtility
  {
    string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Team MediaPortal\\MediaPortal\\FlashGamesSites.xml";

    public void SaveSite(Site site)
    {
      XmlDocument document = new XmlDocument();
      document.Load(path);

      XmlNodeList nodes = document.GetElementsByTagName("site");
      foreach (XmlNode node in nodes)
      {
        if (node.Attributes["name"].Value == site.Name)
        {
          RemoveSite(site.Name);
        }
      }
      document.Load(path);

      XmlElement _site = document.CreateElement("site");
      _site.SetAttribute("name", site.Name);
      _site.SetAttribute("url", site.Url);
      _site.SetAttribute("gameBaseUrl", site.GameBaseUrl);
      document.DocumentElement.AppendChild(_site);

      if (!string.IsNullOrEmpty(site.Description))
      {
        XmlElement description = document.CreateElement("description");
        description.InnerText = site.Description;
        _site.AppendChild(description);
      }
      if (!string.IsNullOrEmpty(site.ImageUrl))
      {
        XmlElement siteImage = document.CreateElement("imageUrl");
        _site.AppendChild(siteImage);
        XmlCDataSection CDataSiteRegex = document.CreateCDataSection(site.ImageUrl);
        siteImage.AppendChild(CDataSiteRegex);
      }
      if (!string.IsNullOrEmpty(site.CategoryRegex))
      {
        XmlElement categoryRegex = document.CreateElement("categoryRegex");
        _site.AppendChild(categoryRegex);
        XmlCDataSection CDataCategoryRegex = document.CreateCDataSection(site.CategoryRegex);
        categoryRegex.AppendChild(CDataCategoryRegex);
      }
      if (!string.IsNullOrEmpty(site.SubCategoryRegex))
      {
        XmlElement subcategoryRegex = document.CreateElement("subcategoryRegex");
        _site.AppendChild(subcategoryRegex);
        XmlCDataSection CDataSubcategoryRegex = document.CreateCDataSection(site.SubCategoryRegex);
        subcategoryRegex.AppendChild(CDataSubcategoryRegex);
      }
      if (!string.IsNullOrEmpty(site.GameRegex))
      {
        XmlElement gameRegex = document.CreateElement("gameRegex");
        _site.AppendChild(gameRegex);
        XmlCDataSection CDataGameRegex = document.CreateCDataSection(site.GameRegex);
        gameRegex.AppendChild(CDataGameRegex);
      }
      if (!string.IsNullOrEmpty(site.GameFileRegex))
      {
        XmlElement gameFileRegex = document.CreateElement("gameFileRegex");
        _site.AppendChild(gameFileRegex);
        XmlCDataSection CDataFileGameRegex = document.CreateCDataSection(site.GameFileRegex);
        gameFileRegex.AppendChild(CDataFileGameRegex);
      }

      document.Save(path);
    }

    public List<Site> ReadSiteXml()
    {
      if (File.Exists(path))
      {

        XmlDocument document = new XmlDocument();
        document.Load(path);
        List<Site> SiteList = new List<Site>();

        XmlNodeList nodes = document.GetElementsByTagName("site");
        foreach (XmlNode node in nodes)
        {
          Site site = new Site();
          site.Name = node.Attributes["name"].Value;
          site.Url = node.Attributes["url"].Value;
          if (node.Attributes.Count > 2)
          site.GameBaseUrl = node.Attributes["gameBaseUrl"].Value;

          foreach (XmlNode element in node.ChildNodes)
          {
            if (element.Name == "description")
              site.Description = element.InnerText;
            else if (element.Name == "imageUrl")
              site.ImageUrl = element.InnerText;
            else if (element.Name == "categoryRegex")
            {
              site.CategoryRegex = element.InnerText;
              site.HasCategories = true;
            }
            else if (element.Name == "subcategoryRegex")
            {
              site.SubCategoryRegex = element.InnerText;
              site.HasSubCategories = true;
            }
            else if (element.Name == "gameRegex")
            {
              site.GameRegex = element.InnerText;
              site.HasGames = true;
            }
            else if (element.Name == "gameFileRegex")
            {
              site.GameFileRegex = element.InnerText;
            }
          }
          SiteList.Add(site);
        }
        return SiteList;
      }
      XmlDocument doc = new XmlDocument();
      XmlElement SitesElement = doc.CreateElement("", "sites", "");
      doc.AppendChild(SitesElement);
      doc.Save(path);

      return null;
    }

    public void RemoveSite(string siteName)
    {
      XmlDocument document = new XmlDocument();
      document.Load(path);
      XmlNode site = document.SelectSingleNode("//site[@name='" + siteName + "']");
      site.ParentNode.RemoveChild(site);
      document.Save(path);
    }
  }
}
